<template>
  <div>
    <div
      style="display: flex; font-weight: bold; color: #000000; justify-content: center; font-size: 29px; padding: 20px">
      地方标准项目建议表
    </div>
    <div style="
        display: flex;
        font-weight: bold;
        color: #000000;
        justify-content: center;
        font-size: 21px;
        margin-bottom: 10px;
      ">
      （试行）
    </div>
    <t-form labelAlign="top" ref="form" :data="formData" class="step-form table-form" labelWidth="140px">
      <t-row>
        <t-col :span="2" class="title"><span class="require">*</span>项目名称</t-col>
        <t-col :span="10">
          <t-form-item name="projectName">
            {{ formData.projectName }}
          </t-form-item>
        </t-col>
      </t-row>
      <t-row>
        <t-col :span="2" class="title"><span class="require">*</span>标准性质</t-col>
        <t-col :span="4">
          <t-form-item name="standardProperty">
            {{ formData.standardProperty | filterByDict(dict.type.standard_property) }}
          </t-form-item>
        </t-col>
        <t-col :span="2" class="title"><span class="require">*</span>标准体系</t-col>
        <t-col :span="4">
          {{ formData.systemId | filterByTree(standardSystemOptions) }}
        </t-col>
      </t-row>
      <t-row>
        <t-col :span="2" class="title"><span class="require">*</span>制定/修订</t-col>
        <t-col :span="4">
          <t-form-item name="formulateType">
            <t-radio-group :disabled="true" clearable v-model="formData.formulateType"
              :options="dict.type.formulate_type">
            </t-radio-group>
          </t-form-item>
        </t-col>
        <t-col :span="2" class="title"><span class="require"
            v-if="formData.formulateType && formData.formulateType !== '1'">*</span>被修订标准号
        </t-col>
        <t-col :span="4">
          <t-form-item v-if="formData.formulateType && formData.formulateType !== '1'" name="formulateType">
            {{ formData.reviseNum }}
          </t-form-item>
        </t-col>
      </t-row>
      <t-row>
        <t-col :span="2" class="title"><span class="require">*</span>技术归口单位</t-col>
        <t-col :span="4"> 标准委员会 </t-col>
        <t-col :span="2" class="title"><span class="require">*</span>主管部门</t-col>
        <t-col :span="4">
          {{ formData.manageDeptId | filterByTree(deptMenuOptions) }}
        </t-col>
      </t-row>
      <t-row>
        <t-col :span="2" class="title"><span class="require">*</span>起草单位</t-col>
        <t-col :span="10">
          <standard-company ref="standard-company" tableSize="small" v-model="formData.draftUnits"></standard-company>
        </t-col>
      </t-row>
      <t-row>
        <t-col :span="2" class="title"><span class="require">*</span>联系人</t-col>
        <t-col :span="4">
          <t-form-item name="linkMan">
            {{ formData.linkMan }}
          </t-form-item>
        </t-col>
        <t-col :span="2" class="title"><span class="require">*</span>联系方式</t-col>
        <t-col :span="4">
          <t-form-item name="linkWay">
            {{ formData.linkWay }}
          </t-form-item>
        </t-col>
      </t-row>
      <t-row>
        <t-col :span="2" class="title"><span class="require">*</span>计划完成时间</t-col>
        <t-col :span="4">
          <t-form-item name="completeDate">
            {{ formData.completeDate }}
          </t-form-item>
        </t-col>
        <t-col :span="2" class="title"><span class="require">*</span>建议表申请时间</t-col>
        <t-col :span="4">
          <t-form-item name="jybApplyDate">
            {{ formData.jybApplyDate }}
          </t-form-item>
        </t-col>
      </t-row>
      <t-row style="min-height: 200px">
        <t-col :span="2" class="title"><span class="require">*</span>目的、意义（标准制修订的背景、必要性和可行性简要论述）</t-col>
        <t-col :span="10">
          <t-form-item name="aim">
            <inner-html :content="formData.aim"></inner-html>
          </t-form-item>
        </t-col>
      </t-row>
      <t-row>
        <t-col :span="2" class="title"><span class="require">*</span>采标情况</t-col>
        <t-col :span="10" style="display: block">
          <t-row>
            <t-col :span="2" class="sub-title"><span class="require">*</span>（1）采用国际标准：</t-col>
            <t-col :span="10">
              <t-form-item name="bidCondition.bidType">
                <t-radio-group :disabled="true" clearable v-model="formData.bidCondition.bidType"
                  :options="dict.type.bid_type">
                </t-radio-group>
              </t-form-item>
            </t-col>
          </t-row>
          <t-row>
            <t-col :span="2" class="sub-title"><span class="require"
                v-if="formData.bidCondition.bidType !== '1'">*</span>（2）采用程度：</t-col>
            <t-col :span="10">
              <t-form-item v-if="formData.bidCondition.bidType !== '1'" name="bidCondition.bidDegree">
                <t-radio-group :disabled="true" clearable v-model="formData.bidCondition.bidDegree"
                  :options="dict.type.bid_degree">
                </t-radio-group>
              </t-form-item>
            </t-col>
          </t-row>
          <t-row>
            <t-col :span="2" class="sub-title"><span class="require"
                v-if="formData.bidCondition.bidType !== '1'">*</span>（3）采标名称：</t-col>
            <t-col :span="10">
              <t-form-item v-if="formData.bidCondition.bidType !== '1'" name="bidCondition.bidName">
                {{ formData.bidCondition.bidName }}
              </t-form-item>
            </t-col>
          </t-row>
          <t-row>
            <t-col :span="2" class="sub-title"><span class="require"
                v-if="formData.bidCondition.bidType !== '1'">*</span>（4）采 标 号：</t-col>
            <t-col :span="10">
              <t-form-item v-if="formData.bidCondition.bidType !== '1'" name="bidCondition.bidNum">
                {{ formData.bidCondition.bidNum }}
              </t-form-item>
            </t-col>
          </t-row>
        </t-col>
      </t-row>
      <t-row style="min-height: 200px">
        <t-col :span="2" class="title"><span class="require">*</span>范围和主要技术内容</t-col>
        <t-col :span="10">
          <t-form-item name="radius">
            <inner-html :content="formData.radius"></inner-html>
          </t-form-item>
        </t-col>
      </t-row>
      <t-row style="min-height: 200px">
        <t-col :span="2" class="title"><span class="require">*</span>国内外情况简要说明</t-col>
        <t-col :span="10">
          <t-form-item name="description">
            <inner-html :content="formData.description"></inner-html>
          </t-form-item>
        </t-col>
      </t-row>
      <t-row style="min-height: 200px">
        <t-col :span="2" class="title"><span class="require">*</span>有关法律法规和强制性标准的关系</t-col>
        <t-col :span="10">
          <t-form-item name="relation">
            <inner-html :content="formData.relation"></inner-html>
          </t-form-item>
        </t-col>
      </t-row>
      <t-row>
        <t-col :span="2" class="title"><span class="require">*</span>是否有科研项目支撑</t-col>
        <t-col :span="2">
          <t-form-item name="projectUp">
            <t-radio-group :disabled="true" clearable v-model="formData.projectUp" :options="dict.type.yes_or_not">
            </t-radio-group>
          </t-form-item>
        </t-col>
        <t-col :span="2" class="title"><span class="require"
            v-if="formData.projectUp && formData.projectUp === '1'">*</span>科研项目编号及名称
        </t-col>
        <t-col :span="6">
          <t-form-item v-if="formData.projectUp && formData.projectUp === '1'" name="projectUpInfos"
            :show-error-message="false">
            <suggest-project ref="suggest-project" table-size="small"
              v-model="formData.projectUpInfos"></suggest-project>
          </t-form-item>
        </t-col>
      </t-row>
      <t-row>
        <t-col :span="2" class="title"><span class="require">*</span>是否涉及专利</t-col>
        <t-col :span="2">
          <t-form-item name="patentUp">
            <t-radio-group :disabled="true" clearable v-model="formData.patentUp" :options="dict.type.yes_or_not">
            </t-radio-group>
          </t-form-item>
        </t-col>
        <t-col :span="2" class="title"><span class="require"
            v-if="formData.patentUp && formData.patentUp === '1'">*</span>专利号及名称
        </t-col>
        <t-col :span="6">
          <t-form-item v-if="formData.patentUp && formData.patentUp === '1'" name="patentUpInfos"
            :show-error-message="false">
            <suggest-patent ref="suggest-patent" table-size="small" v-model="formData.patentUpInfos"></suggest-patent>
          </t-form-item>
        </t-col>
      </t-row>
      <t-row>
        <t-col :span="2" class="title"><span class="require">*</span>相关附件1</t-col>
        <t-col :span="10">
          <t-table v-if="isMyProject" row-key="id" :columns="fileColumns" :data="fileData">
            <template #op="{ row }">
              <file-upload v-if="$store.state.user.roles.includes('dwsby')" @success="uploadSuccess" tips="限制pdf、图片"
                accept="application/pdf,image/*" v-model="row.fileIds" :moduleKey="row.moduleKey"
                :fileList="row.fileLists" :multiple="true">
                <t-button variant="outline">
                  <t-icon name="cloud-upload" slot="icon" />
                  点击上传
                </t-button>
              </file-upload>
            </template>
            <template #c1="{ row }">
              <a v-if="row.c3" class="t-button-link" :href="`${file_domain}/${row.c3}`" target="_blank">{{ row.c1 }}</a>
              <div v-else>{{ row.c1 }}</div>
            </template>
          </t-table>
          <t-table v-else row-key="id" :columns="fileColumns" :data="fileData">
            <template #c1="{ row }">
              <a v-if="row.c3" class="t-button-link" :href="`${file_domain}/${row.c3}`" target="_blank">{{ row.c1 }}</a>
              <div v-else>{{ row.c1 }}</div>
            </template>
            <template #op="{ row }">
              <div v-for="(item, index) in row.fileLists" :key="index">
                <a class="t-button-link" target="_blank" :href="item.url">
                  {{ item.name }}
                </a>
              </div>
            </template>
          </t-table>
        </t-col>
      </t-row>
    </t-form>
  </div>
</template>

<script>
import { VITE_FILE_DOMAIN } from '@/api/upload'
import standardCompany from '@/pages/bzh/standard/components/standard-company.vue';
import suggestPatent from './suggest-patent.vue';
import suggestProject from './suggest-project.vue';
import InnerHtml from '@/components/InnerHtml/index.vue';
import FileUpload from '@/components/FileUpload/index.vue';
import { getFiles } from '@/api/common';

export default {
  name: 'suggest-detail',
  dicts: ['standard_property', 'formulate_type', 'bid_type', 'bid_degree', 'yes_or_not'],
  components: {
    standardCompany,
    suggestPatent,
    suggestProject,
    InnerHtml,
    FileUpload,
  },
  props: {
    objId: [String, Number],
    reviewOpen: {
      type: Boolean,
      default: false,
    },
  },
  data () {
    return {
      file_domain: VITE_FILE_DOMAIN,
      deptMenuOptions: [],
      standardSystemOptions: [],
      formData: {
        projectName: '',
        standardProperty: '',
        formulateType: '',
        reviseNum: '',
        manageDeptId: null,
        linkMan: '',
        linkWay: '',
        draftUnits: [],
        aim: '',
        bidCondition: {
          bidType: '',
          bidDegree: '',
          bidName: '',
          bidNum: '',
        },
        radius: '',
        description: '',
        relation: '',
        projectUp: '',
        patentUp: '',
        projectUpInfos: [],
        patentUpInfos: [],
        systemName: '',
      },
      isMyProject: false,
      fileData: [
        {
          id: 1,
          c1: '盖章后的建议表',
          c2: '是',
          fileIds: [],
          fileLists: [],
          moduleKey: 'bzhJYB',
          require: true,
        },
        {
          id: 2,
          c1: '其他',
          c2: '否',
          fileIds: [],
          fileLists: [],
          moduleKey: 'bzhJYBQT',
          require: false,
        },
      ],
      fileColumns: [
        {
          colKey: 'id',
          title: '序号',
          align: 'center',
          width: 64,
        },
        {
          colKey: 'c1',
          title: '附件名称',
          align: 'center',
        },

        {
          colKey: 'c2',
          title: '是否必需',
          align: 'center',
        },
        {
          colKey: 'op',
          title: '附件列表',
          align: 'left',
        },
      ],
    };
  },
  watch: {
    objId: {
      immediate: true,
      handler (val) {
        if (val) {
          this.getObj(val);
        }
      },
    },
  },
  created () {
    this.getDeptTreeSelect();
    this.getStandardSystemTree();
    // 获取项目附件
    getFiles(this.objId)
      .then((res) => {
        const files = {};
        res.data.forEach((item) => {
          if (!files[item.moduleKey]) {
            files[item.moduleKey] = [];
          }
          files[item.moduleKey].push({
            name: item.fileName,
            url: item.fullFilePath,
            id: item.id,
            filePath: item.filePath,
          });
        });
        this.fileData.forEach((val, index) => {
          this.fileData[index].fileLists = files[val.moduleKey] || [];
          this.fileData[index].fileIds = files[val.moduleKey]?.map((val) => val.id) || [];
        });
      })
      .catch((e) => {
        this.$message.error(e.toString());
      });
  },
  methods: {
    getDeptTreeSelect () {
      return this.$api.system.dept
        .treeSelectAllIn()
        .then((response) => {
          this.deptMenuOptions = response.data;
        })
        .catch((e) => {
          this.$message.error(e.toString());
        });
    },
    getStandardSystemTree () {
      return this.$api.bzh.standard
        .standardSystemTree()
        .then((response) => {
          this.standardSystemOptions = response.data;
        })
        .catch((e) => {
          this.$message.error(e.toString());
        });
    },
    getObj () {
      this.$api.bzh.standard
        .getSuggest(this.objId)
        .then((response) => {
          if (response.data.createUserId === Number(this.$store.state.user.userId)) {
            this.isMyProject = true;
          }
          Object.assign(this.formData, response.data);
        })
        .catch((e) => {
          this.$message.error(e.toString());
        });
    },
    uploadSuccess (ids) {
      this.$api.bzh.standard
        .updateBaseSuggest({
          id: this.objId,
          sysAttMainVo: {
            sysAttMainIds: ids,
          },
        })
        .then((val) => {
          this.$message.success('上传成功');
        })
        .catch((e) => {
          this.$message.error(e.toString());
        });
    },
  },
};
</script>

<style scoped></style>
